package com.fisher.checker;

import static android.os.Environment.DIRECTORY_DOWNLOADS;

import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class FileStorageHelper {
    public static void saveToFileExternalStorage(File appDirectory, String fileName, String content) {
        // 检查外部存储是否可用
        if (isExternalStorageWritable()) {
            File file = new File(appDirectory, fileName);

            try(FileOutputStream fos = new FileOutputStream(file);
                OutputStreamWriter writer = new OutputStreamWriter(fos, StandardCharsets.UTF_8)){
                writer.write(content);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }else{
            throw new RuntimeException("FileExternalStorage not mounted");
        }
    }

    private static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state);
    }
}
